<?php
namespace app\model;

class Game{
    //奖品表
    /**
     * Desc:获取对应条件的游戏的奖品数量
     * User: cbw
     * Time: 2021/1/23 5:05 下午
     * @param $themeid
     * @param string $name
     * @param string $type
     * @return mixed
     */
    public static function getGamePrizeCount($themeid,$name='',$type='')
    {
        $condition = "";
        if ($name){
            $condition .= " and name='".$name."'";
        }
        if ($type){
            $condition .= " and type='".$type."'";
        }
        $sql = " select count(*) as cnt from ".tablename(game_prize)." where themeid=$themeid".$condition;
        return db_find($sql)['cnt'];
    }

    /**
     * Desc:获取对应条件的游戏的奖品列表
     * User: cbw
     * Time: 2021/1/23 5:05 下午
     * @param $themeid
     * @param string $name
     * @param string $type
     * @return mixed
     */
    public static function getGamePrizeList($themeid,$name='',$type='',$page_start=0,$page_size=0)
    {
        $condition = "";
        if ($name){
            $condition .= " and name='".$name."'";
        }
        if ($type){
            $condition .= " and type='".$type."'";
        }
        $condition .= " order by created_at desc  ";
        if ($page_size){
            $condition .= " limit " .$page_start.",".$page_size;
        }

        $sql = " select * from ".tablename(game_prize)." where themeid=$themeid".$condition;
        return db_select($sql);
    }

    /**
     * Desc:插入游戏奖品
     * User: cbw
     * Time: 2021/1/23 5:07 下午
     * @param $data
     * @return false
     */
    public static function insertGamePrize($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(game_prize,$data);
        if (!empty($res)){
            return $insertid;
        }else{
            return false;
        }
    }

    /**
     * Desc:更新游戏奖品
     * User: cbw
     * Time: 2021/1/23 5:08 下午
     * @param $where
     * @param $data
     * @return mixed
     */
    public static function editGamePrizeByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(game_prize,$data,$where);
        return $res;
    }

    /**
     * Desc:通过奖品id查询
     * User: cbw
     * Time: 2021/1/23 5:14 下午
     * @param $id
     * @return mixed
     */
    public static function getGamePrizeItemById($id)
    {
        $res = db_get(game_prize,['p_id'=>$id]);
        return $res;
    }

    //宝箱

    /**
     * Desc:获取查询的宝箱数量
     * User: cbw
     * Time: 2021/1/23 5:24 下午
     * @param $themeid
     * @param $name
     * @return mixed
     */
    public static function getGameBoxCount($themeid,$name='')
    {
        $condition = "";
        if ($name){
            $condition .= " and name='".$name."'";
        }
        $sql = " select count(*) as cnt from ".tablename(game_box)." where themeid=$themeid".$condition;
        return db_find($sql)['cnt'];
    }

    /**
     * Desc:获取宝箱列表
     * User: cbw
     * Time: 2021/1/23 5:23 下午
     * @param $themeid
     * @param string $name
     * @return mixed
     */
    public static function getGameBoxList($themeid,$name='',$page_start=0,$page_size=0)
    {
        $condition = "";
        if ($name){
            $condition .= " and name='".$name."'";
        }
        $condition .= " order by created_at desc  ";
        if ($page_size){
            $condition .= " limit " .$page_start.",".$page_size;
        }
        $sql = " select a.*,b.name as prize_name,b.img from ".tablename(game_box)." as a LEFT JOIN  ".tablename(game_prize)." as b on a.p_id=b.p_id".
            " where a.themeid=$themeid".$condition;
        return db_select($sql);
    }

    /**
     * Desc:获取指定类型的宝箱数量
     * User:cbw
     * Time: 2021/2/26 5:15 上午
     * @param $themeid
     * @param int $type
     * @return mixed
     */
    public static function getGameBoxByType($themeid,$type=0)
    {
        $sql = " select count(*) as cnt from ".tablename(game_box)." where themeid=$themeid and type=$type";
        return db_select($sql)['cnt'];
    }

    /**
     * Desc:获取宝箱已获得的用户数量
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2021/3/16 12:03 上午
     * @param $themeid
     * @param $gb_id
     * @return mixed
     */
    public static function getUserBoxList($themeid,$gb_id)
    {
        $condition = "where b.themeid=$themeid and b.is_end=1 ";
        if ($gb_id){
            $condition .= " and b.gb_id=$gb_id ";
        }
        $sql = " select count(*) as cnt from "."( select a.uid,a.themeid,a.gb_id,a.is_end,a.created_at from (select uid,themeid,gb_id,is_end,created_at from ".tablename(game_user_box)
            ." order by created_at desc ) as a group by a.uid) as b "
            .$condition;
        return db_find($sql)['cnt'];
    }

    /**
     * Desc:插入宝箱
     * User: cbw
     * Time: 2021/1/23 5:23 下午
     * @param $data
     * @return false
     */
    public static function insertGameBox($data){
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(game_box,$data);
        if (!empty($res)){
            return $insertid;
        }else{
            return false;
        }
    }

    /**
     * Desc:更新宝箱信息
     * User: cbw
     * Time: 2021/1/23 5:26 下午
     * @param $where
     * @param $data
     * @return mixed
     */
    public static function editGameBoxByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(game_box,$data,$where);
        return $res;
    }

    /**
     * Desc:获取宝箱通过id
     * User: cbw
     * Time: 2021/1/23 5:27 下午
     * @param $id
     * @return mixed
     */
    public static function getGameBoxItemById($themeid,$id)
    {
        $res = db_get(game_box,['gb_id'=>$id,'themeid'=>$themeid]);
        return $res;
    }

    /**
     * Desc:根据宝箱id获取宝箱以及对应的奖品信息
     * User:cbw
     * Time: 2021/2/3 1:53 下午
     * @param $themeid
     * @param $id
     * @return mixed
     */
    public static function getGameBoxItemAndPrize($themeid,$id)
    {
        $sql = " select a.*,b.p_id,b.name as prize_name,b.img from ".tablename(game_box)." as a LEFT JOIN  ".tablename(game_prize)." as b on a.p_id=b.p_id".
            " where a.themeid=$themeid and a.gb_id=$id";
        return db_find($sql);
    }

    //问题

    /**
     * Desc:获取搜索的问题的数量
     * User:cbw
     * Time: 2021/1/25 11:19 上午
     * @param $themeid
     * @param string $question
     * @return mixed
     */
    public static function getGameQuestionCount($themeid,$question='')
    {
        $condition = "";
        if (!empty($question)){
            $condition .= " and question='".$question."'";
        }
        $sql = " select count(*) as cnt from ".tablename(game_question)." where themeid=$themeid".$condition;
        return db_find($sql)['cnt'];
    }

    /**
     * Desc:获取搜索的问题的列表
     * User:cbw
     * Time: 2021/1/25 11:19 上午
     * @param $themeid
     * @param string $question
     * @param int $page_start
     * @param int $page_size
     * @return mixed
     */
    public static function getGameQuestionList($themeid,$question='',$page_start=0,$page_size=0)
    {
        $condition="";
        if (!empty($question)){
            $condition .= " and question='".$question."'";
        }
        $condition .= " order by created_at desc  ";
        if ($page_size){
            $condition .= " limit " .$page_start.",".$page_size;
        }
        $sql = " select * from ".tablename(game_question)." where themeid=$themeid".$condition;
        return db_select($sql);
    }

    /**
     * Desc:获取全部问题
     * User:cbw
     * Time: 2021/2/4 10:37 下午
     * @param $themeid
     * @return mixed
     */
    public static function getGameAllQuestion($themeid)
    {
        $condition = " order by created_at desc  ";
        $sql = " select * from ".tablename(game_question)." where themeid=$themeid".$condition;
        return db_select($sql);
    }

    public static function insertGameQuestion($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(game_question,$data);
        if (!empty($res)){
            return $insertid;
        }else{
            return false;
        }
    }

    public static function editGameQuestionByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(game_question,$data,$where);
        return $res;
    }

    public static function getGameQuestionItemById($id)
    {
        $res = db_get(game_question,['q_id'=>$id]);
        return $res;
    }

    //游戏关卡
    public static function getGameCount($themeid,$type='',$sort=0)
    {
        $condition = "";
        if ($type){
            $condition .= " and type='".$type."'";
        }
        if ($sort){
            $condition .= " and sort=".$sort;
        }
        $sql = " select count(*) as cnt from ".tablename(game_level)." where themeid=$themeid".$condition;
        return db_find($sql)['cnt'];
    }
    //获取指定的转介绍的关卡item
    public static function getGameItemByOrderAndOpNum($themeid,$num)
    {
        $sql = " select * from ".tablename(game_level). " where themeid=$themeid and type_op_num <=$num order by gl_id desc ";
        return db_find($sql);
    }

    public static function getGameList($themeid,$type='',$page_start=0,$page_size=0,$sort=0)
    {
        $condition = "";
        if ($type){
            $condition .= " and a.type='".$type."'";
        }
        if ($sort){
            $condition .= " and a.sort=".$sort;
        }
        $condition .= " order by a.created_at desc  ";
        if ($page_size){
            $condition .= " limit " .$page_start.",".$page_size;
        }
        $sql = " select a.*,b.name,b.img,b.type as prize_type from ".tablename(game_level).
            " as a LEFT JOIN ".tablename(game_prize)." as b on a.p_id=b.p_id ".
            " where a.themeid=$themeid".$condition;
        return db_select($sql);
    }

    public static function getGameListSort($themeid)
    {
        $condition = " order by a.sort asc  ";
        $sql = " select a.*,b.name,b.img,b.type as prize_type from ".tablename(game_level).
            " as a LEFT JOIN ".tablename(game_prize)." as b on a.p_id=b.p_id ".
            " where a.themeid=$themeid".$condition;
        return db_select($sql);
    }

    public static function insertGame($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(game_level,$data);
        if (!empty($res)){
            return $insertid;
        }else{
            return false;
        }
    }

    public static function editGameByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(game_level,$data,$where);
        return $res;
    }

    public static function getGameItemById($id)
    {
        $res = db_get(game_level,['p_id'=>$id]);
        return $res;
    }

    public static function getGameItemBySort($themeid,$sort)
    {
        $sql = " select a.*,b.name,b.img,b.type as prize_type from ".tablename(game_level).
            " as a LEFT JOIN ".tablename(game_prize)." as b on a.p_id=b.p_id ".
            " where a.themeid=$themeid and a.sort=$sort ";
        $res = db_find($sql);
        return $res;
    }

    public static function getGameListBySorts($themeid,$str)
    {
        $sql = " select a.*,b.name,b.img,b.type as prize_type from ".tablename(game_level).
            " as a LEFT JOIN ".tablename(game_prize)." as b on a.p_id=b.p_id ".
            " where a.themeid=$themeid and a.sort in ( ".$str. " ) ";
        $res = db_select($sql);
        return $res;
    }


    //成长记录表
    public static function getGameLifeCount($themeid,$uid=0,$type='')
    {
        $condition = "";
        if ($type){
            $condition .= " and type='".$type."'";
        }
        if ($uid){
            $condition .= " and fromuid='{$uid}'";
        }
        $sql = " select count(*) as cnt from ".tablename(game_life)." where themeid=$themeid".$condition;
        return db_find($sql)['cnt'];
    }

    public static function getGameLifeList($themeid,$uid=0,$type='',$page_start=0,$page_size=0,$nickname='',$mobile='',$start_day='',$end_day='',$workerid=0,$total=false)
    {
        $condition = "";
        if ($uid!=0){
            $condition .= " and a.fromuid='{$uid}' ";
        }
        if ($type){
            if ($type=='level'){
                $condition .= " and (a.type='".$type."' or a.type='box')";
            }else{
                $condition .= " and a.type='".$type."'";
            }
        }
        if ($nickname){
            $condition .= "  and b.nickname like '".$nickname."' ";
        }
        if ($workerid){
            $condition .= "  and b.workerid = '".$workerid."' ";
        }
        if ($start_day && $end_day){
            $condition .= "  and a.created_at between '".$start_day."' and '".$end_day."' ";
        }
        if ($mobile){
            $condition .= "  and b.mobile='".$mobile."' ";
        }
        $condition .= " order by a.created_at desc  ";


        if ($page_size && !$total){
            $condition .= " limit " .$page_start.",".$page_size;
        }
        if ($total){
            $sql = "select count(*) from ".tablename(game_life).
                " as a LEFT JOIN". tablename(t_fans)." as b on a.uid=b.uid and a.themeid=b.themeid where a.themeid=$themeid".$condition;
            $list = (int)db_fetchcolumn($sql);
        }else{
            $sql = "select a.*,b.username,b.avatar,b.nickname,b.mobile from ".tablename(game_life).
                " as a LEFT JOIN". tablename(t_fans)." as b on a.uid=b.uid and a.themeid=b.themeid where a.themeid=$themeid".$condition;
            $list = db_select($sql);
            if (!empty($list)) {
                foreach ($list as $index => $item) {
                    if (empty($item['avatar']) && empty($item['username'])){
                        $sql = "select b.username,b.avatar,b.nickname,b.mobile from " . tablename(t_fans) . " as b where uid='". $item['uid'] . "' order by username desc limit 1";
                        $fans = db_find($sql);
                        $list[$index]['username']=$fans['username'];
                        $list[$index]['avatar']=$fans['avatar'];
                        $list[$index]['nickname']=$fans['nickname'];
                        $list[$index]['mobile']=$fans['mobile'];
                    }
                }
            }
        }
        return $list;
    }

    public static function insertGameLife($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(game_life,$data);
        if (!empty($res)){
            return $insertid;
        }else{
            return false;
        }
    }

    public static function editGameLifeItemByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(game_life,$data,$where);
        return $res;
    }

    public static function getGameLifeItemByWhere($where)
    {
        $res = db_get(game_life,$where);
        return $res;
    }


    //用户宝箱获取记录

    public static function getGameUserBoxCount($themeid,$uid=0)
    {
        $condition = "";
        if ($uid!=0){
            $condition .= " and uid='{$uid}' ";
        }
        $sql = " select count(*) as cnt from ".tablename(game_user_box)." where themeid=$themeid".$condition;
        return db_find($sql)['cnt'];
    }

    public static function getGameUserBoxList($themeid,$uid=0,$page_start=0,$page_size=0,$nickname='',$mobile='',$start_day='',$end_day='',$box_name='')
    {
        $condition = "";
        if ($uid!=0){
            $condition .= " and a.uid='{$uid}' ";
        }
        if ($nickname){
            $condition .= "  and b.nickname like '".$nickname."' ";
        }
        if ($box_name){
            $condition .= "  and c.name like '".$nickname."' ";
        }
        if ($start_day && $end_day){
            $condition .= "  and a.created_at between '".$start_day."' and '".$end_day."' ";
        }
        if ($mobile){
            $condition .= "  and b.mobile='".$mobile."' ";
        }
        $condition .= " order by a.created_at desc  ";
        if ($page_size){
            $condition .= " limit " .$page_start.",".$page_size;
        }
        $sql = "select a.*,b.username,b.avatar,b.nickname,b.mobile,c.name as box_name,d.question,e.name,e.img from ".tablename(game_user_box).
            " as a LEFT JOIN". tablename(t_fans)." as b on a.uid=b.uid and a.themeid=b.themeid ".
            " LEFT JOIN ".tablename(game_box)." as c on a.gb_id=c.gb_id  and a.themeid = c.themeid".
            " LEFT JOIN ".tablename(game_question)." as d on a.q_id=d.q_id  and a.themeid = c.themeid".
            " LEFT JOIN ".tablename(game_prize)." as e on a.p_id=e.p_id and a.themeid=e.themeid ".
            " where a.themeid=$themeid".$condition;

        return db_select($sql);
    }

    public static function getGameUserBoxFlagList($themeid,$search='',$start_day='',$end_day='',$page_start,$page_size,$gb_id=0)
    {
        $condition1 = " ";
        $condition = " where b.themeid=$themeid and b.is_end=1";
        if ($gb_id){
            $condition .= " and b.gb_id=$gb_id";
        }
        if ($search){
            $condition .= " and c.nickname like '".$search."'"." or c.mobile like '".$search."' ";
        }
        if ($start_day && $end_day){
            $condition .= "  and b.created_at between '".$start_day."' and '".$end_day."'";
        }
        if ($page_size){
            $condition1 = " limit ".$page_start.",".$page_size;
        }

        $sql = " select b.*,c.username,c.avatar,c.nickname,c.mobile,d.name as box_name,e.question,f.name,f.img from ".
            "( select a.gub_id,a.themeid,a.uid,a.gb_id,a.p_id,a.q_id,a.created_at,a.is_end from ( select gub_id,themeid,uid,gb_id,p_id,q_id,created_at,is_end from".
            tablename(game_user_box)." order by created_at desc ) as a group by a.uid) as b".
            " LEFT JOIN ". tablename(t_fans)." as c on b.uid=c.uid and c.themeid=$themeid ".
            " LEFT JOIN ".tablename(game_box)." as d on b.gb_id=d.gb_id and d.themeid = $themeid".
            " LEFT JOIN ".tablename(game_question)." as e on b.q_id=e.q_id and e.themeid = $themeid".
            " LEFT JOIN ".tablename(game_prize)." as f on b.p_id=f.p_id and f.themeid=$themeid "
            .$condition." order by b.created_at desc ".$condition1;
        return db_select($sql);
    }

    public static function insertGameUserBox($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(game_user_box,$data);
        if (!empty($res)){
            return $insertid;
        }else{
            return false;
        }
    }
    public static function getGameUserBoxByWhere($where)
    {
        return db_get(game_user_box,$where);
    }
    public static function editGameUserBoxItemByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(game_user_box,$data,$where);
        return $res;
    }
    //用户宝箱
    public static function getGameUserBoxItemById($themeid,$id,$uid)
    {
        $res = db_get(game_user_box,['gb_id'=>$id,'themeid'=>$themeid,'uid'=>$uid]);
        return $res;
    }

    //获取用户最后一个宝箱奖品
    public static function getUserBoxLastPrize($themeid,$uid)
    {
        $condition = " order by a.created_at desc  ";
        $sql = "select a.*,b.username,b.avatar,b.nickname,b.mobile,c.gb_id,c.name,c.friend_view,d.question,e.name as prize_name from ".tablename(game_user_box).
            " as a LEFT JOIN". tablename(t_fans)." as b on a.uid=b.uid and a.themeid=b.themeid ".
            " LEFT JOIN ".tablename(game_box)." as c on a.gb_id=c.gb_id and a.themeid = c.themeid".
            " LEFT JOIN ".tablename(game_question)." as d on a.q_id=d.q_id  and a.themeid = d.themeid".
            " LEFT JOIN ".tablename(game_prize)." as e on a.p_id=e.p_id  and a.themeid = e.themeid".
            " where a.themeid=$themeid and a.uid='{$uid}'".$condition;

        return db_find($sql);
    }

    //获取用户最后一个宝箱
    public static function getUserLastBox($themeid,$uid)
    {
        $condition = " order by a.created_at desc";
        $sql = " select a.*,b.avatar,b.username,b.nickname,b.mobile,c.gb_id,c.name,c.friend_view from ".tablename(game_user_box).
            " as a LEFT JOIN ".tablename(t_fans)." as b on a.uid=b.uid and a.themeid=b.themeid ".
            " LEFT JOIN ".tablename(game_box)." as c on a.gb_id=c.gb_id and a.themeid=c.themeid".
            " where a.themeid=$themeid and a.uid='{$uid}'".$condition;
        return db_find($sql);
    }

    //用户游戏基础记录

    public static function getGameUserDetailCount($themeid,$uid=0)
    {
        $condition = "";
        if ($uid!=0){
            $condition .= " and uid='{$uid}' ";
        }
        $sql = " select count(*) as cnt from ".tablename(game_user_detail)." where themeid=$themeid".$condition;
        return db_find($sql)['cnt'];
    }

    public static function getGameUserDetailList($themeid,$uid=0,$page_start=0,$page_size=0,$nickname='',$mobile='',$start_day='',$end_day='')
    {
        $condition = "";
        if ($uid!=0){
            $condition .= " and a.uid='{$uid}' ";
        }
        if ($nickname){
            $condition .= "  and b.nickname like '".$nickname."' ";
        }
        if ($start_day && $end_day){
            $condition .= "  and a.created_at between '".$start_day."' and '".$end_day."' ";
        }
        if ($mobile){
            $condition .= "  and b.mobile='".$mobile."' ";
        }
        $condition .= " order by a.created_at desc  ";
        if ($page_size){
            $condition .= " limit " .$page_start.",".$page_size;
        }
        $sql = "select a.*,b.username,b.avatar,b.nickname,b.mobile from ".tablename(game_user_detail).
            " as a LEFT JOIN". tablename(t_fans)." as b on a.uid=b.uid and a.themeid=b.themeid ".
            " where a.themeid=$themeid".$condition;

        return db_select($sql);
    }

    public static function insertGameUserDetail($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(game_user_detail,$data);
        if (!empty($res)){
            return $insertid;
        }else{
            return false;
        }
    }

    public static function editGameUserDetailByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(game_user_detail,$data,$where);
        return $res;
    }

    public static function getGameUserDetail($themeid,$uid)
    {
        $res = db_get(game_user_detail,['themeid'=>$themeid,'uid'=>$uid]);
        return $res;
    }

    //用户游戏关卡记录

    public static function getGameUserLevelCount($themeid,$uid=0)
    {
        $condition = "";
        if ($uid!=0){
            $condition .= " and uid='{$uid}' ";
        }
        $sql = " select count(*) as cnt from ".tablename(game_user_level)." where themeid=$themeid".$condition;
        return db_find($sql)['cnt'];
    }

    public static function getGameUserLevelList($themeid,$uid,$page_start=0,$page_size=0,$nickname='',$mobile='',$start_day='',$end_day='')
    {
        $condition = "";
        if ($uid!=0){
            $condition .= " and b.uid='{$uid}' ";
        }
        if ($nickname){
            $condition .= "  and c.nickname like '".$nickname."' ";
        }
        if ($start_day && $end_day){
            $condition .= "  and b.created_at between '".$start_day."' and '".$end_day."' ";
        }
        if ($mobile){
            $condition .= "  and c.mobile='".$mobile."' ";
        }
        if ($page_size){
            $condition .= " order by a.created_at desc limit ".$page_start.",".$page_size;
        }else{
            $condition .= " order by a.created_at desc ";
        }
        $sql = " select a.current_level,b.uid,b.gl_id,b.created_at,c.username,c.avatar,c.nickname,c.mobile,d.name,d.img from "
            .tablename(game_user_detail)." as a ".
            "";
        $sql = "select a.*,b.username,b.avatar,b.nickname,b.mobile,c.sort,c.type,c.type_op_num,d.name,d.img from "
            .tablename(game_user_level).
            " as a LEFT JOIN". tablename(t_fans)." as b on a.uid=b.uid and a.themeid=b.themeid ".
            " LEFT JOIN". tablename(game_level)." as c on a.gl_id=c.gl_id and a.themeid=c.themeid ".
            " LEFT JOIN ".tablename(game_prize)." as d on c.p_id=d.p_id and a.themeid=d.themeid ".
            " where a.themeid=$themeid".$condition;
        return db_select($sql);
    }

    public static function getLevelUserFlag($themeid,$search='',$start_day='',$end_day='',$page_start,$page_size,$gl_id=0)
    {
        $condition1 = " ";
        $condition = " where a.themeid=$themeid and b.status='end' ";
        if ($gl_id){
            $condition .= "  and b.gl_id=$gl_id";
        }
        if ($search){
            $condition .= " and c.nickname like '".$search."'"." or c.mobile like '".$search."' ";
        }
        if ($start_day && $end_day){
            $condition .= "  and b.created_at between '".$start_day."' and '".$end_day."'";
        }
        if ($page_size){
            $condition1 = " limit ".$page_start.",".$page_size;
        }
        $sql = " select a.current_level,b.uid,b.status,b.gul_id,b.gl_id,b.created_at,c.username,c.avatar,c.nickname,c.mobile,d.sort,d.type_op_num,d.p_id,e.name,e.img from "
            .tablename(game_user_detail)." as a ".
            " left join ".tablename(game_user_level)." as b on a.uid=b.uid and a.themeid=b.themeid and a.current_level=b.gl_id+1".
            " left join ".tablename(t_fans)." as c on a.uid=c.uid and a.themeid=c.themeid ".
            " left join ".tablename(game_level)." as d on b.gl_id=d.gl_id and a.themeid=d.themeid".
            " left join ".tablename(game_prize)." as e on d.p_id=e.p_id and a.themeid=e.themeid "
            .$condition." order by  b.created_at desc ".$condition1;
//        $sql = "select a.*,b.username,b.avatar,b.nickname,b.mobile,c.sort,c.type,c.type_op_num,d.name,d.img from ".tablename(game_user_level).
//            " as a LEFT JOIN". tablename(t_fans)." as b on a.uid=b.uid and a.themeid=b.themeid ".
//            " LEFT JOIN". tablename(game_level)." as c on a.gl_id=c.gl_id and a.themeid=c.themeid ".
//            " LEFT JOIN ".tablename(game_prize)." as d on c.p_id=d.p_id and a.themeid=d.themeid "
//            .$condition." order by a.gul_id desc ".$condition1;
        return db_select($sql);
    }

    public static function getLevelUserList($themeid,$gl_id)
    {
        $condition = "where themeid=$themeid";
        if ($gl_id){
            $condition .= " and current_level=$gl_id";
        }else{
            $condition .= " and current_level>1";
        }
        $sql = " select COUNT(*) as cnt from ".tablename(game_user_detail).$condition ;

        return db_find($sql)['cnt'];
    }


    public static function formatLevelUserList($themeid)
    {
        $arr = [];
        $all = self::getLevelUserList($themeid,0);
        $temp1 = [
            'title'=>'全部',
            'num' =>  $all,
            'gl_id'=>0,
            'url'=>""
        ];
        array_push($arr,$temp1);
        for ($x=1; $x<=10; $x++){
            $level = $x+1;
            $x_num = self::getLevelUserList($themeid,$level);
            if ($x_num) {
                $temp = [
                    'title'=>'第'.$x.'关',
                    'num' =>  $x_num,
                    'gl_id'=>$x,
                    'url'=>"'gl_id:".$x."'"
                ];
                array_push($arr,$temp);
            }

        }
        return $arr;
    }

    public static function formatBoxUserList($themeid)
    {
        $arr = [];
        $box_list = self::getGameBoxList($themeid);
        $all = self::getUserBoxList($themeid,0);
        if ($all){
            array_push($arr,['title'=>'全部','num'=>$all,'gb_id'=>0]);
        }
        $count = count($box_list);
        for ($i=1;$i<=$count;$i++){
            $num = self::getUserBoxList($themeid,$box_list[$i-1]['gb_id']);
            if ($num) {
                $temp = [
                    'title'=>$box_list[$i-1]['name'],
                    'num' =>  $num,
                    'gb_id'=>$box_list[$i-1]['gb_id']
                ];

                array_push($arr,$temp);
            }
        }
        return $arr;
    }

    public static function insertGameUserLevel($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(game_user_level,$data);
        if (!empty($res)){
            return $insertid;
        }else{
            return false;
        }
    }

    public static function editGameUserLevelByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(game_user_level,$data,$where);
        return $res;
    }

    public static function getGameUserLevelByWhere($where)
    {
        $res = db_get(game_user_level,$where);
        return $res;
    }

    //用户游戏奖品记录

    public static function getGameUserPrizeCount($themeid,$uid=0)
    {
        $condition = "";
        if ($uid!=0){
            $condition .= " and uid='{$uid}' ";
        }
        $sql = " select count(*) as cnt from ".tablename(game_user_prize)." where themeid=$themeid and type='level' ".$condition;
        return db_find($sql)['cnt'];
    }

    public static function getGameUserPrizeList($themeid,$uid=0)
    {
        $condition = "";
        if ($uid!=0){
            $condition .= " and a.uid='{$uid}' ";
        }
        $condition .= " order by created_at desc  ";
        $sql = "select a.*,b.username,b.avatar,b.nickname,b.mobile,c.name,c.img from ".tablename(game_user_prize).
            " as a LEFT JOIN". tablename(t_fans)." as b on a.uid=b.uid and a.themeid=b.themeid ".
            " LEFT JOIN". tablename(game_prize)." as c on a.p_id=c.p_id and a.themeid=c.themeid ".
            " where a.themeid=$themeid".$condition;

        return db_select($sql);
    }
    public static function getGameUserPrizeFlagByWhere($where)
    {
        $res = db_get(game_user_prize,$where);
        return $res;
    }

    public static function insertGameUserPrize($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(game_user_prize,$data);
        if (!empty($res)){
            return $insertid;
        }else{
            return false;
        }
    }

    public static function getGameUserLastPrize($themeid,$uid)
    {
        $condition = " order by created_at desc  ";
        $sql = "select a.*,b.username,b.avatar,b.nickname,b.mobile,c.name,c.img from ".tablename(game_user_prize).
            " as a LEFT JOIN". tablename(t_fans)." as b on a.uid=b.uid and a.themeid=b.themeid ".
            " LEFT JOIN". tablename(game_prize)." as c on a.p_id=c.p_id and a.themeid=c.themeid ".
            " where a.themeid=$themeid and a.type='level' and a.uid='{$uid}'".$condition;
        return db_find($sql);
    }


    //基础配置参数检查
    public static function BaseCheck($theme,$config,$system)
    {
        $msg = checkTheme($theme);
        if (!$msg){

        }
        if ($msg){
            return $msg;
        }else{
            return '';
        }
    }

    /**
     * Desc:获取用户游戏是否开始 最大关卡 最大宝箱
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2021/3/19 12:54 上午
     * @param $themeid
     * @param $uid
     * @return array
     */
    public static function apiGetUserGameAndBoxInfo($themeid,$uid)
    {
        $start_game = false;
        $current_level = 0;
        $max_box = '';
        $game_user_detail_item = self::getGameUserDetail($themeid,$uid);
        if($game_user_detail_item){
            $start_game = true;
            $current_level = $game_user_detail_item['current_level']-1;
            $user_last_box_flag = self::getUserLastBox($themeid,$uid);
            if ($user_last_box_flag){
                $max_box = $user_last_box_flag['name'];
            }
        }
        return ['start_game'=>$start_game,'current_level'=>$current_level,'max_box'=>$max_box];
    }
    //判断是否显示新手引导
    public static function getShowLead($themeid,$uid)
    {
        $item = self::getGameUserDetail($themeid,$uid);
        if (!$item){
            //不存在创建
            self::NewGameUserUpdateInfo($themeid,$uid);
            return true;
        }else{
            $show_lead = $item['read_lead'];
            if ($show_lead==1){
                return false;
            }elseif ($show_lead==2){
                $update_item = self::editGameUserDetailByWhere(['gb_id'=>$item['gb_id']],['read_lead'=>1]);
                return true;
            }
        }
    }
    //新游戏用户创建记录
    public static function NewGameUserUpdateInfo($themeid,$uid)
    {
        $first_level_item = self::getGameItemById(1);
        //更新用户游戏基础记录
        $detail_data = [
            'read_lead'=>1,
            'themeid'=>$themeid,
            'current_level'=>1,
            'current_level_type'=>$first_level_item['type'],
            'friend_total_num'=>0,
            'friend_need_num'=>0,
            'friend_remain_num'=>0,
            'share_total_num'=>0,
            'share_need_num'=>0,
            'share_remain_num'=>0,
            'order_total_num'=>0,
            'order_need_num'=>0,
            'order_remain_num'=>0,
            'can_open_gb_id'=>0,
            'open_gb'=>0,
            'show_last_tips'=>0,
            'uid'=>$uid,
        ];
        switch ($first_level_item['type']){
            case 'share':
                $detail_data['share_need_num']=$first_level_item['type_op_num'];
                $detail_data['share_remain_num']=$first_level_item['type_op_num'];
                break;
            case 'friend':
                $detail_data['friend_need_num']=$first_level_item['type_op_num'];
                $detail_data['friend_remain_num']=$first_level_item['type_op_num'];
                break;
            case 'order':
                $detail_data['order_need_num']=$first_level_item['type_op_num'];
                $detail_data['order_remain_num']=$first_level_item['type_op_num'];
                break;
            default:
                break;
        }
        $update_detail = self::insertGameUserDetail($detail_data);
        //更新用户游戏关卡记录
        $flag_data = [
            'themeid'=>$themeid,
            'uid'=>$uid,
            'gl_id'=>1,
            'status'=>'process',
        ];
        $update_flag = self::insertGameUserLevel($flag_data);
    }

    public static function checkHasUserDetail($themeid,$uid)
    {
        $user_detail = self::getGameUserDetail($themeid,$uid);
        if (!$user_detail){
            $level_item = self::getGameItemBySort(1,1);
            $data = [
                'themeid'=>$themeid,
                'current_level'=>1,
                'current_level_type'=>$level_item['type'],
                'friend_total_num'=>0,
                'friend_need_num'=>$level_item['type_op_num'],
                'friend_remain_num'=>$level_item['type_op_num'],
            ];
            $insert_item = self::insertGameUserDetail($data);
        }
    }
    //判断游戏是否闯关完成
    public static function checkGameOver($themeid,$uid)
    {
        $user_game_detail = self::getGameUserDetail($themeid,$uid);
        $current_level = $user_game_detail['current_level'];
        $level_type = $user_game_detail['current_level_type'];
        if ($current_level!=10){
            return false;
        }else{
            $remain_num = $user_game_detail['friend_remain_num'];
            switch ($level_type){
                case 'friend':
                    $remain_num = $user_game_detail['friend_remain_num'];
                    break;
                case 'share':
                    $remain_num = $user_game_detail['share_remain_num'];
                    break;
                case 'order':
                    $remain_num = $user_game_detail['order_remain_num'];
                    break;
                default:
                    break;
            }
            if ($remain_num>0){
                return false;
            }else{
                return true;
            }
        }
    }
    //获取顶部提示语
    public static function getSwiperTips($themeid,$uid)
    {
        $swiper_tips = '';
        $user_game_detail = self::getGameUserDetail($themeid,$uid);
        $current_level = $user_game_detail['current_level'];
        $pre_level = $current_level - 1;
        if ($pre_level>=1){
            $game_level_info = self::getGameItemBySort($themeid,$pre_level);
        }else{
            $game_level_info = [];
        }

        if ($current_level>1 && $current_level<10){
            $swiper_tips = '恭喜您通过第'.($current_level-1).'关'." 获得".$game_level_info['name'].'请继续努力加油！';
        }elseif ($current_level==10){
            $game_over = self::checkGameOver($themeid,$uid);
            if ($game_over){
                $swiper_tips = '恭喜您通关！';
            }
        }
        $top_swiper_tips = [];
        $friend_swiper_tips = Game::getFriendLevelList($themeid,$uid);
        $end_game = self::checkUserEndGame($themeid,$uid);
        if ($end_game){
            array_push($top_swiper_tips,'恭喜您！通过全部游戏关卡');
        }
        if ($current_level>0 && $current_level<=10){
            array_push($top_swiper_tips,'通过此关，随机可能获得神秘大礼');
        }
        if (count($friend_swiper_tips)>0){
            $top_swiper_tips =$friend_swiper_tips;
            array_unshift($top_swiper_tips,$swiper_tips);
        }else{
//            return '';
        }
        return $top_swiper_tips;
    }

    /**
     * Desc:获取关卡以及共享宝箱的进度
     * User:cbw
     * Time: 2021/2/28 10:12 下午
     * @param $themeid
     * @param $uid
     * @return array
     */
    public static function getSwiperLevelAndBoxInfo($themeid,$uid)
    {
        $swipers = [];
        $current_game_user_detail_item = Game::getGameUserDetail($themeid,$uid);
        $current_level = $current_game_user_detail_item['current_level'];
        $current_level_type = $current_game_user_detail_item['current_level_type'];
        $current_level_op_info = Game::getLevelOpNum($current_level_type,$current_game_user_detail_item);
        $need_num = $current_level_op_info['need_num'];
        $remain_num = $current_level_op_info['remain_num'];
        $temp1= ['type'=>'level','current_level'=>$current_level,'need_num'=>$need_num,'remain_num'=>$remain_num];
        array_push($swipers,$temp1);

        $last_box_flag = self::getUserLastBox($themeid,$uid);
        if ($last_box_flag && !$last_box_flag['is_end']){
            $remain_num_box = $last_box_flag['remain_friend_view'];
            switch ($last_box_flag['gb_id']){
                case 1:
                    $box_prize_info = self::getGameBoxItemAndPrize($themeid,1);
                    $need_num_box = $box_prize_info['friend_view'];
                    $img = 'https://css2020.yixuan.net/2020css/game/icxz1.png';
                    break;
                case 2:
                    $box_prize_info = self::getGameBoxItemAndPrize($themeid,2);
                    $img = 'https://css2020.yixuan.net/2020css/game/icxz2.png';
                    $need_num_box = $box_prize_info['friend_view'];
                    break;
                case 3:
                    $box_prize_info = self::getGameBoxItemAndPrize($themeid,3);
                    $img = 'https://css2020.yixuan.net/2020css/game/icxz3.png';
                    $need_num_box = $box_prize_info['friend_view'];
                    break;
                default:
                    break;
            }
            $temp1= ['type'=>'box','img'=>$img,'need_num'=>$need_num_box,'remain_num'=>$remain_num_box];
            array_push($swipers,$temp1);
        }
        return $swipers;
    }
    //获取当前关卡的操作次数
    public static function getLevelOpNum($level_type,$user_detail_item)
    {
        $level = $user_detail_item['current_level'];
        $themeid = $user_detail_item['themeid'];
        $new_need_num = 0;
        if ($level>1){
            $prev_item = self::getGameItemBySort($themeid,$level-1);
            $current_level_item = self::getGameItemBySort($themeid,$level);
            if ($prev_item && $current_level_item){
                if ($prev_item['type']==$user_detail_item['current_level_type']){
                    $new_need_num = $current_level_item['type_op_num']-$prev_item['type_op_num'];
                }
            }
        }else{
            $prev_item = [];
        }
        if ($level_type=='share'){
            $total_num = $user_detail_item['share_total_num'];
            $need_num = $user_detail_item['share_need_num'];
            $remain_num = $user_detail_item['share_remain_num'];
        }elseif ($level_type=='friend'){
            $total_num = $user_detail_item['friend_total_num'];
            $need_num = $user_detail_item['friend_need_num'];
            $remain_num = $user_detail_item['friend_remain_num'];
        }elseif ($level_type=='order'){
            $total_num = $user_detail_item['order_total_num'];
            $need_num = $user_detail_item['order_need_num'];
            $remain_num = $user_detail_item['order_remain_num'];
        }
        if ($prev_item && $new_need_num && $current_level_item){
            if ($new_need_num>0){
                $need_num = $new_need_num;
            }
        }
        return [
            'total_num'=>$total_num,
            'need_num'=>$need_num,
            'remain_num'=>$remain_num,
        ];
    }
    //获取用户的当前关卡的奖品
    public static function getUserCurrentLevelPrize($themeid,$sort)
    {
        $sql = "select a.themeid,a.p_id,b.name,b.img from ".tablename(game_level).
            " as a LEFT JOIN".tablename(game_prize)." as b on a.p_id=b.p_id and a.themeid=b.themeid ".
            " where a.themeid=$themeid and a.sort=$sort";
        return db_find($sql);
    }

    public static function getLevelOpInfo($themeid,$level,$need_num,$remain_num,$user_detail_item,$has=true)
    {
        $old_need_num = 0;
        $tips = ['first'=>'','second'=>'','third'=>'','four'=>'','five'=>''];
        $item = self::getGameItemBySort($themeid,$level);
        if ($level>1){
            $prev_item = self::getGameItemBySort($themeid,$level-1);
        }else{
            $prev_item = [];
        }
        if ($prev_item){
            if ($prev_item['type']==$item['type']){
                $old_need_num = $need_num;
                $need_num = $item['type_op_num']-$prev_item['type_op_num'];
            }
        }
        if ($level==1){
            $old_need_num = $need_num;
        }

        switch ($item['type']){
            case 'share':
                $tips['first'] = '需分享给';
                $tips['second'] = $need_num;
                $tips['third'] = '位好友,还需分享';
                $tips['four'] = max(0,$user_detail_item['share_remain_num']);
                $tips['five'] = '位好友';
                if (!$has){
                    $tips['third'] = '位好友';
                    $tips['four'] = '';
                    $tips['five'] = '';
                }
                break;
            case 'friend':
                $tips['first'] = '需';
                $tips['second'] = $need_num;
                $tips['third'] = '位好友访问，还需邀请';
                $tips['four'] = max(0,$user_detail_item['friend_remain_num']);
                $tips['five'] = '位好友访问';
                if (!$has){
                    $tips['third'] = '位好友访问';
                    $tips['four'] = '';
                    $tips['five'] = '';
                }
                break;
            case 'order':
                $tips['first'] = '需转介绍';
                $tips['second'] = $need_num;
                $tips['third'] = '位好友下单，还需转介绍';
                $tips['four'] = max(0,$user_detail_item['order_remain_num']);
                $tips['five'] = '位好友下单';
                if (!$has){
                    $tips['third'] = '位好友下单';
                    $tips['four'] = '';
                    $tips['five'] = '';
                }
                break;
            default:
                break;
        }
        return $tips;
    }
    //获取关卡以及宝箱信息
    public static function getUserGameRoute($themeid,$uid,$user_detail_item)
    {
        $route = [];
        $current_level = $user_detail_item['current_level'];
        $wait_open_item = [];
        $levels = self::getGameListSort($themeid);
        foreach ($levels as $k => $v){
            $item = [];
            if ($v['sort']==$current_level){
                $getLevelOpNum = self::getLevelOpNum($user_detail_item['current_level_type'],$user_detail_item);
                if ($current_level==10){
                    if ($user_detail_item['show_last_tips']==-1){
                        $item = [
                            'type'=>1,
                            'status'=>2,
                            'other_id'=>$v['p_id'],
                            'other_img'=>$v['img'],
                            'other_name'=>$v['name'],
                            'title'=>'~ 恭喜您通关 ~',
                            'bind_sorts'=>$v['bind_show_sorts']?true:false,
                            'bind_sort_info'=>self::formatBindSorts($v),
                            'content'=>['first'=>'恭喜您通关！']
                        ];
                    }else{
                        $item = [
                            'type'=>1,
                            'status'=>2,
                            'other_id'=>$v['p_id'],
                            'other_img'=>$v['img'],
                            'other_name'=>$v['name'],
                            'title'=>'第'.$v['sort'].'关通关法则',
                            'bind_sorts'=>$v['bind_show_sorts']?true:false,
                            'bind_sort_info'=>self::formatBindSorts($v),
                            'content'=>self::getLevelOpInfo($themeid,$v['sort'],$v['type_op_num'],$getLevelOpNum['remain_num'],$user_detail_item)
                        ];
                    }
                    array_push($route,$item);
                }else{
                    $item = [
                        'type'=>1,
                        'status'=>2,
                        'other_id'=>$v['p_id'],
                        'other_img'=>$v['img'],
                        'other_name'=>$v['name'],
                        'title'=>'第'.$v['sort'].'关通关法则',
                        'bind_sorts'=>$v['bind_show_sorts']?true:false,
                        'bind_sort_info'=>self::formatBindSorts($v),
                        'content'=>self::getLevelOpInfo($themeid,$v['sort'],$v['type_op_num'],$getLevelOpNum['remain_num'],$user_detail_item)
                    ];
                    array_push($route,$item);
                }

            }elseif ($v['sort']<$current_level){
                $item = [
                    'type'=>1,
                    'status'=>1,
                    'other_id'=>$v['p_id'],
                    'other_img'=>$v['img'],
                    'other_name'=>$v['name'],
                    'title'=>'恭喜您，完成第'.$current_level.'关',
                    'bind_sorts'=>$v['bind_show_sorts']?true:false,
                    'bind_sort_info'=>self::formatBindSorts($v),
                    'content'=>'获得'.$v['name']
                ];
                array_push($route,$item);
            }elseif ($v['sort']>$current_level){
                $item = [
                    'type'=>1,
                    'status'=>3,
                    'other_id'=>$v['p_id'],
                    'other_img'=>$v['img'],
                    'other_name'=>$v['name'],
                    'title'=>'第'.$v['sort'].'关通关法则',
                    'bind_sorts'=>$v['bind_show_sorts']?true:false,
                    'bind_sort_info'=>self::formatBindSorts($v),
                    'content'=>self::getLevelOpInfo($themeid,$v['sort'],$v['type_op_num'],$v['type_op_num'],$user_detail_item,false)
                ];
                array_push($route,$item);
            }
            if ($k==3){
                $user_has_box = self::getGameUserBoxItemById($themeid,1,$uid);
                $box_prize_info = self::getGameBoxItemAndPrize($themeid,1);
                $box_type='default';
                $need_share = false;
                if (!$user_has_box){
                    $status = 3;
                    $share_remain = '';
                }else{
                    if ($user_has_box['is_shares']==1){
                        $box_type = 'shares';
                        if ($user_has_box['is_end']){
                            $status = 1;
                        }else{
                            $status=2;
                            $need_share = true;
                        }
                    }else{
                        $status = 1;
                    }
                    $share_remain = '还需邀请'.$user_has_box['remain_friend_view'].'位好友访问';
                }
                if ($current_level==5 && !$user_has_box){
                    $status=2;
                }
                $item = [
                    'type'=>2,
                    'status'=>$status,
                    'other_id'=>1,
                    'box_type'=>$box_type,
                    'need_share'=>$need_share,
                    'other_img'=>$box_prize_info['img'],
                    'other_name'=>$box_prize_info['prize_name'],
                    'title'=>$box_prize_info['name'].'开启条件',
                    'content'=>'完成第'.($k+1).'关后，正确回答开箱问题',
                    'share_remain'=>$share_remain,
                ];
                if ($status==2 && empty($wait_open_item)){
                    $wait_open_item = $item;
                }
                array_push($route,$item);
            }
            if ($k==4){
                $user_has_box = [];
                $user_has_box = self::getGameUserBoxItemById($themeid,2,$uid);
                $box_prize_info = self::getGameBoxItemAndPrize($themeid,2);
                $box_type='default';
                $need_share = false;
                if (!$user_has_box){
                    $status = 3;
                    $share_remain = '';
                }else{
                    if ($user_has_box['is_shares']==1){
                        $box_type = 'shares';
                        if ($user_has_box['is_end']){
                            $status = 1;
                        }else{
                            $status=2;
                            $need_share = true;
                        }
                    }else{
                        $status = 1;
                    }
                    $share_remain = '还需邀请'.$user_has_box['remain_friend_view'].'位好友访问';
                }
                if ($current_level==6 && !$user_has_box){
                    $status=2;
                }
                $item = [
                    'type'=>2,
                    'status'=>$status,
                    'other_id'=>2,
                    'box_type'=>$box_type,
                    'need_share'=>$need_share,
                    'other_img'=>$box_prize_info['img'],
                    'other_name'=>$box_prize_info['prize_name'],
                    'title'=>$box_prize_info['name'].'开启条件',
                    'content'=>'完成第'.($k+1).'关后，正确回答开箱问题',
                    'share_remain'=>$share_remain,
                ];
                if ($status==2 && empty($wait_open_item)){
                    $wait_open_item = $item;
                }
                array_push($route,$item);
            }
            if ($k==5){
                $user_has_box = [];
                $user_has_box = self::getGameUserBoxItemById($themeid,3,$uid);
                $box_prize_info = self::getGameBoxItemAndPrize($themeid,3);
                $box_type='default';
                $need_share = false;
                if (!$user_has_box){
                    $status = 3;
                    $share_remain = '';
                }else{
                    if ($user_has_box['is_shares']==1){
                        $box_type = 'shares';
                        if ($user_has_box['is_end']){
                            $status = 1;
                        }else{
                            $status=2;
                            $need_share = true;
                        }
                    }else{
                        $status = 1;
                    }
                    $share_remain = '还需邀请'.$user_has_box['remain_friend_view'].'位好友访问';
                }
                if ($current_level==7 && !$user_has_box){
                    $status=2;
                }
                $item = [
                    'type'=>2,
                    'status'=>$status,
                    'other_id'=>3,
                    'box_type'=>$box_type,
                    'need_share'=>$need_share,
                    'other_img'=>$box_prize_info['img'],
                    'other_name'=>$box_prize_info['prize_name'],
                    'title'=>$box_prize_info['name'].'开启条件',
                    'content'=>'完成第'.($k+1).'关后，正确回答开箱问题',
                    'share_remain'=>$share_remain,
                ];
                if ($status==2 && empty($wait_open_item)){
                    $wait_open_item = $item;
                }
                array_push($route,$item);
            }
        }
        return ['route'=>$route,'wait_open_item'=>$wait_open_item];
    }
    //成长记录中获取类型文字
    public static function getLevelTypeName($level_type)
    {
        $name = '次好友分享';
        switch ($level_type){
            case 'share':
                break;
            case 'friend':
                $name='人访问';
                break;
            case 'order':
                $name='人下单';
                break;
            default:
                break;
        }
        return $name;
    }
    //获取成长记录中的关卡信息
    public static function formatCurrentLevelInfo($themeid,$current_game_user_detail_item)
    {
        $current_level = $current_game_user_detail_item['current_level'];
        $current_level_type = $current_game_user_detail_item['current_level_type'];
        $op_num_arr = self::getLevelOpNum($current_level_type,$current_game_user_detail_item);
        $current_level_info = [
            'level_num'=>$current_game_user_detail_item['current_level'],
            'level_type_name'=>Game::getLevelTypeName($current_game_user_detail_item['current_level_type']),
            'prize_name'=>Game::getUserCurrentLevelPrize($themeid,$current_level)['name'],
            'has_friend_num'=>$op_num_arr['total_num'],
            'need_friend_num'=>$op_num_arr['need_num'],
            'remain_friend_num'=>$op_num_arr['remain_num']
        ];
        return $current_level_info;
    }

    /**
     * Desc:获取共享宝箱的信息 成长记录中
     * User:cbw
     * Time: 2021/2/28 11:27 下午
     * @param $themeid
     * @param $uid
     * @return array
     */
    public static function formatSharesBoxInfo($themeid,$uid)
    {
        $has_shares_box = false;
        $box_name = '';
        $prize_name='';
        $has_friend_num = '';
        $need_friend_num = '';
        $remain_friend_num = '';
        $last_box_prize = self::getUserBoxLastPrize($themeid,$uid);
        if ($last_box_prize && !$last_box_prize['is_end']){
            $has_shares_box = true;
            $box_name = $last_box_prize['name'];
            $prize_name = $last_box_prize['prize_name'];
            $has_friend_num =($last_box_prize['friend_view']-$last_box_prize['remain_friend_view']);
            $need_friend_num=$last_box_prize['friend_view'];
            $remain_friend_num=$last_box_prize['remain_friend_view'];
        }
        return ['has_shares_box'=>$has_shares_box,
                'box_name'=>$box_name,
                'prize_name'=>$prize_name,
                'level_type_name'=>'访问',
            'has_friend_num'=>$has_friend_num,
            'need_friend_num'=>$need_friend_num,
            'remain_friend_num'=>$remain_friend_num
            ];
    }

    //获取成长记录s
    public static function formatLifeInfo($themeid,$uid,$page_start,$page_size,$type='',$workerid=0)
    {
        $current_game_user_detail_item = Game::getGameUserDetail($themeid,$uid);
//        $invite_total = self::getGameLifeCount($themeid,$uid,'friend');
//        $invite_total= self::getGameLifeList($themeid,$uid,'friend',$page_start,$page_size,'','','','',$workerid,true);
        $invite_total = $current_game_user_detail_item['friend_total_num'];
//        $order_total = self::getGameLifeCount($themeid,$uid,'order');
        $order_total= $current_game_user_detail_item['order_total_num'];
        $level_total= self::getGameLifeList($themeid,$uid,'level',$page_start,$page_size,'','','','',$workerid,true);

        $list = [];
        $get_items = self::getGameLifeList($themeid,$uid,$type,$page_start,$page_size,'','','','',$workerid);
        $total     = self::getGameLifeList($themeid,$uid,$type,$page_start,$page_size,'','','','',$workerid,true);
        foreach ($get_items as $k => $v){
            array_push($list,self::formatListItem($v));
        }
        return ['invite_total'=>$invite_total,'order_total'=>$order_total,'level_total'=>$level_total,'list'=>$list,'total'=>$total];
    }
    //格式化list-item
    public static function formatListItem($item)
    {
        $avatar = isset($item['avatar'])?$item['avatar']:'';
        $username = isset($item['username'])?$item['username']:'';
        $mobile = isset($item['mobile'])?$item['mobile']:'';
        $title = isset($item['nickname'])?$item['nickname']:'';
        $time = isset($item['created_at'])?$item['created_at']:'';
        $type = isset($item['type'])?$item['type']:'';
        $tips = '';
        $icon = '';
        switch ($item['type']){
            case 'share':
                $tips = '邀请+1';
                $icon = 'https://css2020.yixuan.net/2020css/game/ic15.png';
                break;
            case 'friend':
                $tips = '好友访问+1';
                $icon = 'https://css2020.yixuan.net/2020css/game/pc18.png';
                break;
            case 'level':
                $tips = $item['title'];
                $icon = 'https://css2020.yixuan.net/2020css/game/ic40.png';
                break;
            case 'box':
                $tips = $item['title'];
                $icon = 'https://css2020.yixuan.net/2020css/game/ic40.png';
                break;
            case 'order':
                $tips = '转介绍+1';
                $icon = 'https://css2020.yixuan.net/2020css/game/ic41.png';
                break;
            default:
                break;
        }
        return ['avatar'=>$avatar,'title'=>$title,'username'=>$username,'mobile'=>$mobile,'time'=>$time,'type'=>$type,'tips'=>$tips,'icon'=>$icon];
    }

    public static function getGameMethod()
    {
        $temp_arr = [
            ['num'=>1,'icon'=>'https://css2020.yixuan.net/2020css/game/pc20.png','title'=>'分享好友','content'=>'将本链接分享给好友好友打开','content_tips'=>'访问即可+1'],
            ['num'=>2,'icon'=>'https://css2020.yixuan.net/2020css/game/pc21.png','title'=>'分享好友','content'=>'将本链接分享给好友好友打开','content_tips'=>'访问即可+1']
        ];
        return $temp_arr;
    }

    //礼品信息包含通关和宝箱
    public static function getPrizeInfo($themeid,$uid)
    {
        $has_level_prize = false;
        $has_box_prize = false;
        $level_prize = [];
        $box_prize = [];
        $prize_count = self::getGameUserPrizeCount($themeid,$uid);
        $box_count = self::getGameUserBoxCount($themeid,$uid);
        if ($prize_count>0){
            $has_level_prize = true;
            $last_level_prize = self::getGameUserLastPrize($themeid,$uid);
            $level_prize = [
                'level_name'=>$last_level_prize['sort'],
                'prize_name'=>$last_level_prize['name']
            ];
        }
        if ($box_count>0){
            $has_box_prize = true;
            $last_box_prize = self::getUserBoxLastPrize($themeid,$uid);
            $box_prize = [
                'prize_name'=>mb_substr($last_box_prize['prize_name'],0,20),
                'gb_id'=>$last_box_prize['gb_id'],
                'box_name'=>$last_box_prize['name']
            ];
        }
        return [
            'has_level_prize'=>$has_level_prize,
            'has_box_prize'=>$has_box_prize,
            'level_prize'=>$level_prize,
            'box_prize'=>$box_prize
        ];
    }

    /**
     * Desc:格式化获取团成员信息
     * User:cbw
     * Time: 2021/2/28 1:41 下午
     * @param $themeid
     * @param $uid
     * @param $friend_uid
     * @return array
     */
    public static function getFriendPrizeInfo($themeid,$uid,$friend_uid)
    {
        $has_box_prize = false;
        $box_prize = [];
        $box_count = self::getGameUserBoxCount($themeid,$uid);
        if ($box_count>0){
            $has_box_prize = true;
            $last_box_prize = self::getUserBoxLastPrize($themeid,$uid);
            $box_prize = [
                'prize_name'=>$last_box_prize['prize_name'],
                'box_name'=>$last_box_prize['name'],
                'box_id'=>$last_box_prize['gb_id']
            ];
        }

        $friend_has_level = false;
        $friend_has_box = false;
        $friend_level_prize = [];
        $friend_box_prize = [];
        $can_shares_box = false;
        $shares_box_tips = "共享宝箱福利";

        $friend_prize_count = self::getGameUserPrizeCount($themeid,$friend_uid);
        $friend_box_count = self::getGameUserBoxCount($themeid,$friend_uid);
        if ($friend_prize_count>0){
            $friend_has_level = true;
            $friend_last_level_prize = self::getGameUserLastPrize($themeid,$friend_uid);
            $friend_level_prize = [
                'level_name'=>$friend_last_level_prize['sort'],
                'level_num'=>$friend_last_level_prize['sort'],
                'prize_name'=>$friend_last_level_prize['name']
            ];
        }
        if ($friend_box_count>0){
            $friend_has_box = true;
            $friend_last_box_prize = self::getUserBoxLastPrize($themeid,$friend_uid);
            $friend_box_prize = [
                'prize_name'=>$friend_last_box_prize['prize_name'],
                'box_name'=>$friend_last_box_prize['name'],
                'box_id'=>$friend_last_box_prize['gb_id']
            ];
        }
        if (!$has_box_prize && $friend_has_box){
            $can_shares_box = true;
        }elseif ($has_box_prize && $friend_has_box && $friend_box_prize['box_id']>$box_prize['box_id'] && $last_box_prize['is_end']==1){
            $can_shares_box = true;
        }
        return [
            'friend_has_level'=>$friend_has_level,
            'friend_has_box'=>$friend_has_box,
            'friend_level_prize'=>$friend_level_prize,
            'friend_box_prize'=>$friend_box_prize,
            'can_shares_box'=>$can_shares_box,
            'shares_box_tips'=>$shares_box_tips,
        ];
    }
    //获取随机问题
    public static function formatRandQuestion($themeid)
    {
        $res = ['title'=>'','q_id'=>0,'selects'=>[]];
        $max = (int)self::getGameQuestionCount($themeid);
        (int)$rand_max = $max-1;
        $questions = Game::getGameAllQuestion($themeid);
        $key = rand(0,$rand_max);
        $question = $questions[$key];
        $title = $question['question'];
        $selects = [];
        $item_a = ['check'=>false,'name'=>'A:'.$question['first'],'select_id'=>1];
        $item_b = ['check'=>false,'name'=>'B:'.$question['second'],'select_id'=>2];
        $item_c = ['check'=>false,'name'=>'C:'.$question['third'],'select_id'=>3];
        array_push($selects,$item_a);
        array_push($selects,$item_b);
        array_push($selects,$item_c);
        $res['title']=$title;
        $res['q_id'] = $question['q_id'];
        $res['selects']=$selects;
        return $res;
    }
    //获取错误选项提示语
    public static function getErrorTips($question)
    {
        $tips = '回答错误！正确答案是：';
        switch ($question['sure_method']){
            case 1:
                $tips .= 'A:'.$question['first'];
                break;
            case 2:
                $tips .= 'B:'.$question['second'];
                break;
            case 3:
                $tips .= 'C:'.$question['third'];
                break;
            default:
                break;
        }
        return $tips;
    }
    //获取错误答案数据选项结构数据
    public static function getErrorSelects($question,$sid,$sure_id)
    {
        $selects = [];
        if ($sid==1){
            $item_a = ['check'=>'error','name'=>'A:'.$question['first'],'select_id'=>1];
            if ($sure_id==2){
                $item_b = ['check'=>true,'name'=>'B:'.$question['second'],'select_id'=>2];
                $item_c = ['check'=>false,'name'=>'C:'.$question['third'],'select_id'=>3];
            }else{
                $item_b = ['check'=>false,'name'=>'B:'.$question['second'],'select_id'=>2];
                $item_c = ['check'=>true,'name'=>'C:'.$question['third'],'select_id'=>3];
            }
        }elseif ($sid==2){
            $item_b = ['check'=>'error','name'=>'B:'.$question['second'],'select_id'=>2];
            if ($sure_id==1){
                $item_a = ['check'=>true,'name'=>'A:'.$question['first'],'select_id'=>1];
                $item_c = ['check'=>false,'name'=>'C:'.$question['third'],'select_id'=>3];
            }else{
                $item_a = ['check'=>false,'name'=>'A:'.$question['first'],'select_id'=>1];
                $item_c = ['check'=>true,'name'=>'C:'.$question['third'],'select_id'=>3];
            }
        }else{
            $item_c = ['check'=>'error','name'=>'C:'.$question['third'],'select_id'=>3];
            if ($sure_id==2){
                $item_b = ['check'=>true,'name'=>'B:'.$question['second'],'select_id'=>2];
                $item_a = ['check'=>false,'name'=>'A:'.$question['first'],'select_id'=>1];
            }else{
                $item_b = ['check'=>false,'name'=>'B:'.$question['second'],'select_id'=>2];
                $item_a = ['check'=>true,'name'=>'A:'.$question['first'],'select_id'=>1];
            }
        }
        array_push($selects,$item_a);
        array_push($selects,$item_b);
        array_push($selects,$item_c);
        return $selects;
    }
    //获取判断百宝箱答案返回数据
    public static function checkAnswerSure($themeid,$config,$uid,$qid,$sid,$other_id)
    {
        $question = self::getGameQuestionItemById($qid);
        $box_prize = self::getGameBoxItemAndPrize($themeid,$other_id);
        if ($question['sure_method']==(int)$sid){
            $answer_status = true;
            $true_info = [
                'p_name'=>$box_prize['prize_name'],
                'title'=>$box_prize['name'],
                'btm_tips'=>$config['btm_tips'],
                'btn_txt'=>$config['btn_txt']
            ];
            $false_info = ['question'=>''];
            $selects = [];

            $where = ['themeid'=>$themeid,'uid'=>$uid,'gb_id'=>$other_id];
            $has_box_question = self::getGameUserBoxByWhere($where);
            if (!$has_box_question){
                //插入成长记录表
                $life_data = [
                    'themeid'=>$themeid,
                    'uid'=>$uid,
                    'fromuid'=>$uid,
                    'icon'=>'https://css2020.yixuan.net/2020css/game/ic15.png',
                    'tips'=>'宝箱+1',
                    'title'=>'获得'.$box_prize['name'],
                    'type'=>'box'
                ];
                self::insertGameLife($life_data);
                //插入宝箱获取记录
                $box_data = [
                    'themeid'=>$themeid,
                    'uid'=>$uid,
                    'gb_id'=>$other_id,
                    'p_id'=>$box_prize['p_id'],
                    'q_id'=>$qid
                ];
                self::insertGameUserBox($box_data);
                //插入奖品记录
                $prize_data = [
                    'themeid'=>$themeid,
                    'uid'=>$uid,
                    'p_id'=>$box_prize['p_id'],
                    'sort'=>$other_id,
                    'type'=>'box',
                    'date'=>date("Y-m-d",time())
                ];
                self::insertGameUserPrize($prize_data);
            }


        }else{
            $answer_status = false;
            $true_info = [];
            $false_info = ['question'=>self::getErrorTips($question)];
            $selects = self::getErrorSelects($question,$sid,$question['sure_method']);
        }
        return ['answer_status'=>$answer_status,'true_info'=>$true_info,'false_info'=>$false_info,'selects'=>$selects];
    }
    //关卡问题回答
    public static function checkLevelAnswerSure($themeid,$config,$uid,$qid,$sid)
    {
        $question = self::getGameQuestionItemById($qid);
        $user_detail = self::getGameUserDetail($themeid,$uid);
        $current_user_level = $user_detail['current_level'];
        $next_level = $current_user_level+1;
        if ($question['sure_method']==(int)$sid){
            $answer_status = true;
            $true_info = [
                'btm_tips'=>$config['btm_tips'],
                'btn_txt'=>$config['btn_txt']
            ];
            $false_info = ['question'=>''];
            $selects = [];

            $where = ['themeid'=>$themeid,'uid'=>$uid,'gl_id'=>$next_level];
            $has_level_box_question = self::checkUserLevelQuestion($where);
            if (!$has_level_box_question){
                //插入关卡问题回答记录
                $box_data = [
                    'themeid'=>$themeid,
                    'uid'=>$uid,
                    'gl_id'=>$next_level,
                    'q_id'=>$qid
                ];
                self::insertGameLevelQuestion($box_data);
            }
        }else{
            $answer_status = false;
            $true_info = [];
            $false_info = ['question'=>self::getErrorTips($question)];
            $selects = self::getErrorSelects($question,$sid,$question['sure_method']);
        }
        return ['answer_status'=>$answer_status,'true_info'=>$true_info,'false_info'=>$false_info,'selects'=>$selects];
    }
    //获取我的奖品中的兑换方式信息
    public static function getConvertMethod($theme,$config)
    {
        $convert_method = [
            'main_shop_name'=>$theme['jubf'],
            'convert_start_time'=>date("Y/m/d",$config['convert_time_begintime']),
            'convert_end_time'=>date("Y/m/d",$config['convert_time_endtime']),
            'convert_type'=>$config['convert_type']?$config['convert_type']:'',
            'convert_address'=>$config['convert_address']?$config['convert_address']:'',
            'convert_tips'=>self::getGameRules($config['game_rules'],'game_rule_content'),
            'method_tips'=>self::getGameRules($config['play_rules'],'play_rule_content'),
        ];
        return $convert_method;
    }

    public static function getGameRules($arr,$key)
    {
        $temp = [];
        if (is_array($arr)){
            foreach ($arr as $k => $v){
                array_push($temp,$v[$key]);
            }
        }
        return $temp;
    }

    /**
     * Desc:获取用户信息
     * User:cbw
     * Time: 2021/2/3 3:03 下午
     * @param $uid
     * @return mixed
     */
    public static function getUserInfoById($uid)
    {
        $res = db_get(t_fans,['uid'=>$uid]);
        return $res;
    }

    /**
     * Desc:判断是否是新用户
     * User: cbw
     * Time: 2021/1/9 2:55 下午
     * @param $fans
     * @return bool
     */
    public static function checkNewUser($fans)
    {
        $is_new_user = false;
        if (!$fans['username'] || !$fans['mobile']){
            $is_new_user = true;
        }
        return $is_new_user;
    }

    /**
     * Desc:对应的操作修改用户游戏基础信息 以及插入游戏成长记录表
     * User:cbw
     * Time: 2021/2/6 3:27 上午
     * @param $themeid
     * @param $uid 当前用户的uid
     * @param int $from_uid 上一级uid
     * @param string $type share分享  friend邀请好友访问  order转介绍
     */
    public static function addUserGameLifeFlag($themeid,$uid,$from_uid=0,$type='share')
    {
        $config =   Theme::getConfig($themeid);
        $isworker = false;
        if ($config['game_out_worker']){
            //上一级非员工记录
            $isworker = Worker::getThemeWorkerByUid($from_uid,$themeid,1);
        }
        if (!$isworker){
            //获取用户基础记录
            if ($type=='share'||$type=='box'){
                $user_detail = self::getGameUserDetail($themeid,$uid);
                $where = [
                    'themeid'=>$themeid,
                    'uid'=>$uid,
                    'gd_id'=>$user_detail['gd_id']
                ];
            }else{
                $user_detail = self::getGameUserDetail($themeid,$from_uid);
                $where = [
                    'themeid'=>$themeid,
                    'uid'=>$from_uid,
                    'gd_id'=>$user_detail['gd_id']
                ];
            }

            switch ($type){
                case 'share':
                    $old_share_total_num = $user_detail['share_total_num'];
                    $new_share_total_num = $old_share_total_num+1;
                    $old_share_remain_num = $user_detail['share_remain_num'];
                    $new_share_remain_num = max(0,($old_share_remain_num-1));
                    $update_data = [
                        'share_total_num'=>$new_share_total_num,
                        'share_remain_num'=>$new_share_remain_num,
                    ];
                    //更新用户关卡基础信息
                    self::editGameUserDetailByWhere($where,$update_data);
                    //插入用户成长记录
                    $insert_data = [
                        'themeid'=>$themeid,
                        'uid'=>$uid,
                        'fromuid'=>$uid,
                        'icon'=>'https://css2020.yixuan.net/2020css/game/ic15.png',
                        'tips'=>'分享+1',
                        'title'=>'',
                        'type'=>'share'
                    ];
                    self::insertGameLife($insert_data);
                    break;
                case 'box':
                    //插入用户成长记录
//                $insert_data = [
//                    'themeid'=>$themeid,
//                    'uid'=>$uid,
//                    'fromuid'=>$uid,
//                    'icon'=>'https://css2020.yixuan.net/2020css/game/ic15.png',
//                    'tips'=>'宝箱+1',
//                    'title'=>'获得宝箱',
//                    'type'=>'box'
//                ];
//                self::insertGameLife($insert_data);
                    break;
                case 'friend':
                    //判断是否有到访记录
                    $has_friend_flag = self::getGameLifeItemByWhere(['themeid'=>$themeid,'fromuid'=>$from_uid,'uid'=>$uid,'type'=>'friend']);

                    if (!$has_friend_flag){
                        $old_friend_total_num = $user_detail['friend_total_num'];
                        $new_friend_total_num = $old_friend_total_num+1;
                        $old_friend_remain_num = $user_detail['friend_remain_num'];
                        $new_friend_remain_num = max(0,($old_friend_remain_num-1));
                        $update_data = [
                            'friend_total_num'=>$new_friend_total_num,
                            'friend_remain_num'=>$new_friend_remain_num,
                        ];
                        //判断用户是否存在未完成的共享宝箱
                        $has_shares_box_flag = self::getUserBoxLastPrize($themeid,$uid);
                        if ($has_shares_box_flag && $has_shares_box_flag['is_shares']==1 && $has_shares_box_flag['is_end']==0)
                        {
                            $new_is_end = 0;
                            $old_remain_friend_view = $has_shares_box_flag['remain_friend_view'];
                            $new_remain_friend_view = max(0,($old_remain_friend_view-1));
                            if ($new_remain_friend_view==0){
                                $new_is_end = 1;
                            }
                            $where1 = ['gub_id'=>$has_shares_box_flag['gub_id'],'themeid'=>$themeid];
                            $edit_data = ['remain_friend_view'=>$new_remain_friend_view,'is_end'=>$new_is_end];
                            $edit_item = self::editGameUserBoxItemByWhere($where1,$edit_data);
                        }
                        //更新用户关卡基础信息
                        self::editGameUserDetailByWhere($where,$update_data);
                        $new_has_friend_flag = self::getGameLifeItemByWhere(['themeid'=>$themeid,'fromuid'=>$from_uid,'uid'=>$uid,'type'=>'friend']);

                        if (!$new_has_friend_flag){
                            //插入用户成长记录
                            $insert_data = [
                                'themeid'=>$themeid,
                                'uid'=>$uid,
                                'fromuid'=>$from_uid,
                                'icon'=>'https://css2020.yixuan.net/2020css/game/pc18.png',
                                'tips'=>'访问+1',
                                'title'=>'',
                                'type'=>'friend'
                            ];
                            self::insertGameLife($insert_data);
                        }
                    }
                    break;
                case 'order':
                    $old_order_total_num = $user_detail['order_total_num'];
                    $new_order_total_num = $old_order_total_num+1;
                    $old_order_remain_num = $user_detail['order_remain_num'];
                    $new_order_remain_num = max(0,($old_order_remain_num-1));
                    $update_data = [
                        'order_total_num'=>$new_order_total_num,
                        'order_remain_num'=>$new_order_remain_num,
                    ];
                    //更新用户关卡基础信息
                    $res = self::editGameUserDetailByWhere($where,$update_data);
                    //插入用户成长记录
                    $insert_data = [
                        'themeid'=>$themeid,
                        'uid'=>$uid,
                        'fromuid'=>$from_uid,
                        'icon'=>'https://css2020.yixuan.net/2020css/game/ic41.png',
                        'tips'=>'转介绍+1',
                        'title'=>'',
                        'type'=>'order'
                    ];
                    self::insertGameLife($insert_data);
                    //增加转介绍跳转到指定的关卡
                    //用户游戏关卡的转介绍的总数
                    $user_order_flags = self::getGameUserDetail($themeid,$from_uid)['order_total_num'];
                    //对应的转介绍的总数对应的关卡
                    $level_item = self::getGameItemByOrderAndOpNum($themeid,$user_order_flags);
                    $update_data1 = [
                        'current_level'=>$level_item['sort'],
                        'current_level_type'=>'order'
                    ];
                    self::editGameUserDetailByWhere($where,$update_data1);
                    break;
                default:
                    break;
            }
        }

    }
    //游戏用户进入判断游戏进度以及相关更新
    public static function checkUserLevel($themeid,$uid)
    {
        $user_detail = self::getGameUserDetail($themeid,$uid);
        $current_user_level = (int)$user_detail['current_level'];
        $current_user_share_total = $user_detail['share_total_num'];
        $current_user_friend_total = $user_detail['friend_total_num'];
        $current_user_order_total = $user_detail['order_total_num'];

        $current_level_info = self::getGameItemBySort($themeid,$current_user_level);
        $current_level_type = $current_level_info['type'];
        $current_level_type_op_num = $current_level_info['type_op_num'];
        //判断关卡等级是否变更
        switch ($current_level_type){
            case 'share':
                if ($current_level_type_op_num!=$user_detail['share_need_num']){
                    if ($current_level_type_op_num>$user_detail['share_need_num']){
                        $new_diff_need_num = int($current_level_type_op_num-$user_detail['share_need_num']);
                        $where_edit_user_level_flag = [
                            'themeid'=>$themeid,
                            'gd_id'=>$user_detail['gd_id']
                        ];
                        $update_user_level_flag = [
                            'share_need_num'=>$current_level_type_op_num,
                            'share_remain_num'=>$user_detail['share_remain_num']+$new_diff_need_num
                        ];
                    }elseif ($current_level_type_op_num<$user_detail['share_need_num']){
                        $used_num = $user_detail['share_need_num']-$user_detail['share_remain_num'];
                        if ($current_level_type_op_num<=$used_num){
                            $where_edit_user_level_flag = [
                                'themeid'=>$themeid,
                                'gd_id'=>$user_detail['gd_id']
                            ];
                            $update_user_level_flag = [
                                'share_need_num'=>$current_level_type_op_num,
                                'share_remain_num'=>0
                            ];
                        }else{
                            $new_diff_need_num = $current_level_type_op_num - $used_num;
                            $where_edit_user_level_flag = [
                                'themeid'=>$themeid,
                                'gd_id'=>$user_detail['gd_id']
                            ];
                            $update_user_level_flag = [
                                'share_need_num'=>$current_level_type_op_num,
                                'share_remain_num'=>$new_diff_need_num
                            ];
                        }

                    }
                    self::editGameUserDetailByWhere($where_edit_user_level_flag,$update_user_level_flag);
                }
                break;
            case 'friend':
                if ($current_level_type_op_num!=$user_detail['friend_need_num']){
                    if ($current_level_type_op_num>$user_detail['friend_need_num']){
                        $new_diff_need_num = int($current_level_type_op_num-$user_detail['friend_need_num']);
                        $where_edit_user_level_flag = [
                            'themeid'=>$themeid,
                            'gd_id'=>$user_detail['gd_id']
                        ];
                        $update_user_level_flag = [
                            'friend_need_num'=>$current_level_type_op_num,
                            'friend_remain_num'=>$user_detail['friend_remain_num']+$new_diff_need_num
                        ];
                    }elseif ($current_level_type_op_num<$user_detail['friend_need_num']){
                        $used_num = $user_detail['friend_need_num']-$user_detail['friend_remain_num'];
                        if ($current_level_type_op_num<=$used_num){
                            $where_edit_user_level_flag = [
                                'themeid'=>$themeid,
                                'gd_id'=>$user_detail['gd_id']
                            ];
                            $update_user_level_flag = [
                                'friend_need_num'=>$current_level_type_op_num,
                                'friend_remain_num'=>0
                            ];
                        }else{
                            $new_diff_need_num = $current_level_type_op_num - $used_num;
                            $where_edit_user_level_flag = [
                                'themeid'=>$themeid,
                                'gd_id'=>$user_detail['gd_id']
                            ];
                            $update_user_level_flag = [
                                'friend_need_num'=>$current_level_type_op_num,
                                'friend_remain_num'=>$new_diff_need_num
                            ];
                        }

                    }
                    self::editGameUserDetailByWhere($where_edit_user_level_flag,$update_user_level_flag);
                }
                break;
            case 'order':
                if ($current_level_type_op_num!=$user_detail['order_need_num']){
                    if ($current_level_type_op_num>$user_detail['order_need_num']){
                        $new_diff_need_num = intval($current_level_type_op_num-$user_detail['order_need_num']);
                        $where_edit_user_level_flag = [
                            'themeid'=>$themeid,
                            'gd_id'=>$user_detail['gd_id']
                        ];
                        $update_user_level_flag = [
                            'order_need_num'=>$current_level_type_op_num,
                            'order_remain_num'=>$user_detail['order_remain_num']+$new_diff_need_num
                        ];
                    }elseif ($current_level_type_op_num<$user_detail['order_need_num']){
                        $used_num = $user_detail['order_need_num']-$user_detail['order_remain_num'];
                        if ($current_level_type_op_num<=$used_num){
                            $where_edit_user_level_flag = [
                                'themeid'=>$themeid,
                                'gd_id'=>$user_detail['gd_id']
                            ];
                            $update_user_level_flag = [
                                'order_need_num'=>$current_level_type_op_num,
                                'order_remain_num'=>0
                            ];
                        }else{
                            $new_diff_need_num = $current_level_type_op_num - $used_num;
                            $where_edit_user_level_flag = [
                                'themeid'=>$themeid,
                                'gd_id'=>$user_detail['gd_id']
                            ];
                            $update_user_level_flag = [
                                'order_need_num'=>$current_level_type_op_num,
                                'order_remain_num'=>$new_diff_need_num
                            ];
                        }

                    }
                    self::editGameUserDetailByWhere($where_edit_user_level_flag,$update_user_level_flag);
                }
                break;
            default:
                break;
        }
        //判断当前关卡是否完成
        $can_next_level = false;
        if ($current_level_type=='share'){
            if ($current_user_share_total>=$current_level_type_op_num){
                $can_next_level = true;
            }
        }elseif ($current_level_type=='friend'){
            if ($current_user_friend_total>=$current_level_type_op_num){
                $can_next_level = true;
            }
        }elseif ($current_level_type=='order'){
            if ($current_user_order_total>=$current_level_type_op_num){
                $can_next_level = true;
            }
        }
        if ($can_next_level){
            if ($current_user_level==5){
                //判断翡翠宝箱是否获取
                $user_has_box = self::getGameUserBoxItemById($themeid,1,$uid);
                if (!$user_has_box){
                    return true;
                }
            }elseif ($current_user_level==6){
                //判断铂金宝箱是否获取
                $user_has_box = self::getGameUserBoxItemById($themeid,2,$uid);
                if (!$user_has_box){
                    return true;
                }
            }elseif ($current_user_level==7){
                //判断钻石宝箱是否获取
                $user_has_box = self::getGameUserBoxItemById($themeid,3,$uid);
                if (!$user_has_box){
                    return true;
                }
            }
            //将当前关卡的进度改为end//更新用户游戏关卡记录
            $find_data = [
                'themeid'=>$themeid,
                'uid'=>$uid,
                'gl_id'=>$current_user_level,
                'status'=>'process'
            ];
            $level_flag = self::getGameUserLevelByWhere($find_data);
            if ($level_flag){
                $where_flag = [
                    'gul_id'=>$level_flag['gul_id']
                ];
                $data_flag = ['status'=>'end'];
                $update_game_user_level = self::editGameUserLevelByWhere($where_flag,$data_flag);
            }
            $next_level = $current_user_level+1;
            $user_next_level_info = self::getGameItemBySort($themeid,$next_level);
            //下一关是问题关卡  当前关卡信息已更新或者插入 跳过
            if (!$user_next_level_info['open_question']){
                $where_data = ['themeid'=>$themeid,'uid'=>$uid,'p_id'=>$current_level_info['p_id'],'sort'=>$current_user_level,'type'=>'level'];
                $check_user_has_prize_flag = self::getGameUserPrizeFlagByWhere($where_data);
                if (!$check_user_has_prize_flag){
                    //插入用户通关奖品
                    $user_level_prize_data = [
                        'themeid'=>$themeid,
                        'uid'=>$uid,
                        'p_id'=>$current_level_info['p_id'],
                        'sort'=>$current_user_level,
                        'type'=>'level',
                        'date'=>date("Y-m-d",time())
                    ];
                    $insert_user_game_level_prize = self::insertGameUserPrize($user_level_prize_data);
                    //通关插入用户成长记录
                    $insert_data = [
                        'themeid'=>$themeid,
                        'uid'=>$uid,
                        'fromuid'=>$uid,
                        'icon'=>'https://css2020.yixuan.net/2020css/game/ic40.png',
                        'tips'=>'通关+1',
                        'title'=>'通过第'.$current_user_level.'关',
                        'type'=>'level'
                    ];
                    self::insertGameLife($insert_data);
                }

            }


            if ($current_user_level==10){
                if ($user_detail['show_last_tips']==0){
                    //最后一关
                    $where = [
                        'themeid'=>$themeid,
                        'uid'=>$uid,
                        'gd_id'=>$user_detail['gd_id']
                    ];
                    $update_data = [
                        'show_last_tips'=>1,
                    ];
                    self::editGameUserDetailByWhere($where,$update_data);
                }
            }

            $next_level = $current_user_level+1;
            $is_end = false;
            if ($next_level>10){
                $is_end = true;
            }
            if (!$is_end){

                $next_level_info = self::getGameItemBySort($themeid,$next_level);
                $next_level_type = $next_level_info['type'];
                $next_level_type_op_num = $next_level_info['type_op_num'];
                //判断新关卡是否存在游戏关卡记录
                $new_insert_level_flag = [
                    'themeid'=>$themeid,
                    'uid'=>$uid,
                    'gl_id'=>$next_level_info['gl_id'],
                    'status'=>'process'
                ];
                $has_next_level_flag = self::getGameUserLevelByWhere($new_insert_level_flag);
                //新关卡插入用户游戏关卡记录
                if (!$has_next_level_flag){
                    self::insertGameUserLevel($new_insert_level_flag);
                }

                //判断下一关是否为问题关卡
                if ($next_level_info['open_question']){
                    $where = ['themeid'=>$themeid,'uid'=>$uid,'gl_id'=>$next_level_info['gl_id']];
                    $has_level_question = self::checkUserLevelQuestion($where);
                    if (!$has_level_question){
                        return true;
                    }
                }
                //更新用户游戏基础记录
                $old_share_total_num = $user_detail['share_total_num'];
                $old_friend_total_num = $user_detail['friend_total_num'];
                $old_order_total_num = $user_detail['order_total_num'];
                $where = [
                    'themeid'=>$themeid,
                    'uid'=>$uid,
                    'gd_id'=>$user_detail['gd_id']
                ];
                if ($next_level_type=='share'){
                    $update_data = [
                        'share_need_num'=>$next_level_type_op_num,
                        'share_remain_num'=>max(0,($next_level_type_op_num-$old_share_total_num)),
                    ];
                }elseif ($next_level_type=='friend'){
                    $update_data = [
                        'friend_need_num'=>$next_level_type_op_num,
                        'friend_remain_num'=>max(0,($next_level_type_op_num-$old_friend_total_num)),
                    ];
                }elseif ($next_level_type=='order'){
                    $update_data = [
                        'order_need_num'=>$next_level_type_op_num,
                        'order_remain_num'=>max(0,($next_level_type_op_num-$old_order_total_num)),
                    ];
                }
                $update_data['current_level']=$next_level;
                $update_data['current_level_type']=$next_level_type;
                $update_data['show_last_tips']=1;
                self::editGameUserDetailByWhere($where,$update_data);
            }

        }
    }

    //判断用户是否通关
    public static function checkUserEndGame($themeid,$uid)
    {
        return db_get(game_user_level,['themeid'=>$themeid,'uid'=>$uid,'gl_id'=>10,'status'=>'end']);
    }

    //插入用户游戏关卡问题回答记录
    public static function insertGameLevelQuestion($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(game_level_question,$data);
        if (!empty($res)){
            return $insertid;
        }else{
            return false;
        }
    }

    //查询用户是否存在关卡问题记录
    public static function checkUserLevelQuestion($where)
    {
        $res = db_get(game_level_question,$where);
        return $res;
    }

    /**
     * Desc:获取进行中的活动列表
     * User:cbw
     * Time: 2021/2/7 3:28 下午
     * @return mixed
     */
    public static function getProcessThemeList()
    {
        $sql = "SELECT * FROM " . tablename(t_theme) . " where status>=0 ORDER BY id,status desc";
        $row = db_select($sql);
        return $row;
    }

    /**
     * Desc:获取指定类型 活动item
     * User:cbw
     * Time: 2021/2/7 4:24 下午
     * @param string $type
     * @return array
     */
    public static function getThemeListByType($type='five_koi',$themeid)
    {
        $temp_arr = [];
        $list = self::getProcessThemeList();
        foreach ($list as $k => $v)
        {
            if ($v['id']!=$themeid){
                if ($type=='five_koi'){
                    $config = Theme::getConfig($v['id']);
                    if ($config['koi_lottery_main']==1){
                        $theme = Theme::getThemeById($v['id']);
                        array_push($temp_arr,$theme);
                    }
                }
                if ($type=='lz_game'){
                    $config = Theme::getConfig($v['id']);
                    if ($config['koi_lottery_main']!=1){
                        $theme = Theme::getThemeById($v['id']);
                        array_push($temp_arr,$theme);
                    }
                }
            }
        }
        return ['type_theme_total'=>count($temp_arr),'type_theme_list'=>$temp_arr];
    }

    /**
     * Desc:获取顶部滑动文字
     * User:cbw
     * Time: 2021/2/7 6:44 下午
     * @param $themeid
     * @param $uid
     * @return array
     */
    public static function getFriendLevelList($themeid,$uid)
    {
        $tips = [];
        $sql = " select a.themeid,b.uid,b.nickname,b.mobile from ".tablename(game_life)." as a ".
            " LEFT JOIN ".tablename(t_fans)." as b on a.uid=b.uid and a.themeid=b.themeid ".
            " where a.uid='{$uid}' or a.fromuid='{$uid}' and a.themeid=$themeid and a.type='friend' group by a.uid";
        $res = db_select($sql);

        $uids = '';
        $uids_arr = [];
        foreach ($res as $k =>$v){
            if ($v['uid']!=$uid){
                $friend_level_detail = Game::getGameUserDetail($themeid,$v['uid']);
                if ($friend_level_detail){
                    array_push($uids_arr,$v['uid']);
                }
            }
        }
        //判断团员 //排除上述好友
        $sql  = "SELECT * FROM " . tablename (t_record) .
            " WHERE themeid=$themeid and uid='{$uid}' and type<>8 order by id desc limit 1";
        $team_info = db_find($sql);
        if ($team_info){
            $pid = $team_info['pid'];
            $sql =  "SELECT * FROM  " . tablename ( t_record ) .
                "  WHERE (type = 0 or type = 1) and status > 0 and pid =$pid order by type asc,id asc";
            $list = db_select ($sql);
            if (count($list)>1){
                foreach ($list as $k => $v)
                {
                    if ($v['uid']!=$uid){
                        $friend_uids .= $v['uid'].',';
                        if (!$uids_arr){
                            array_push($uids_arr,$v['uid']);
                        }else{
                            if (!in_array($v['uid'],$uids_arr)){
                                array_push($uids_arr,$v['uid']);
                            }
                        }
                    }
                }
            }
        }


        if ($uids_arr){
            $uids = implode(",",$uids_arr);
        }

        if ($uids){
            $uids = rtrim($uids,',');

            $new_sql = " select a.current_level,a.uid,b.nickname,b.mobile from ".tablename(game_user_detail)." as a ".
                " LEFT JOIN ".tablename(t_fans)." as b on a.themeid=b.themeid and b.uid=a.uid".
                "  where a.themeid=$themeid and a.uid in( ". $uids." ) order by a.current_level limit 5";
            $new_res = db_select($new_sql);

            shuffle($new_res);
            $format_arr = $new_res;
            if ($format_arr){
                foreach ($format_arr as $k => $v)
                {
                    $temp_level = (int)$v['current_level'];
                    if ($temp_level>1){
                        $show_level = $temp_level-1;
                        $level_prize_info = self::getGameItemBySort($themeid,$show_level);

                        $strlen = mb_strlen($v['nickname'], 'utf-8');
                        if ($strlen<7){
                            $user_name = $v['nickname'];
                        }else{
                            $user_name = mb_substr($v['nickname'], 0, 6, 'utf-8').'***';
                        }
                        $temp_tips = '您的好友'.$user_name.'通过第'.$show_level.'关,获得'.$level_prize_info['name'];
                        array_push($tips,$temp_tips);
                    }
                }
            }
        }

        return $tips;
    }

    /**
     * Desc:共享宝箱订阅消息推送
     * User:cbw
     * Email:17854288794@163.com
     * Time: 2021/3/17 4:05 下午
     * @param $themeid
     * @param $uid
     */
    public static function checkTeamUserAndSendWxMsg($themeid,$uid,$fans,$system)
    {
        $uids = '';
        $friend_uids = '';
        $uids_arr = [];

        $sql  = "SELECT * FROM " . tablename (t_record) .
            " WHERE themeid=$themeid and uid='{$uid}' and type<>8 order by id desc limit 1";
        $team_info = db_find($sql);

        if ($team_info){
            $pid = $team_info['pid'];
            $sql =  "SELECT * FROM  " . tablename ( t_record ) .
                "  WHERE (type = 0 or type = 1) and status > 0 and pid =$pid order by type asc,id asc";
            $list = db_select ($sql);
            if (count($list)>1){
                foreach ($list as $k => $v)
                {
                    if ($v['uid']!=$uid){
                        $friend_uids .= $v['uid'].',';
                        if (!$uids_arr){
                            array_push($uids_arr,$v['uid']);
                        }else{
                            if (!in_array($v['uid'],$uids_arr)){
                                array_push($uids_arr,$v['uid']);
                            }
                        }
                    }
                }
            }
        }
        if ($uids_arr){
            $uids = implode(",",$uids_arr);
        }
        if ($uids) {
            //获取订阅消息未发送列表
            $theme_list = Game::getThemeListByType('five_koi',$themeid);
            $five_theme = Theme::getThemeById($theme_list['type_theme_list'][0]['id']);
            $no_send_sub_msg_list = Koi::getKoiSubList($five_theme['id'],0,1);

            $no_send_uids = [];
            foreach ($no_send_sub_msg_list as $kk => $vv){
                array_push($no_send_uids,$vv['uid']);
            }

            $uids = rtrim($uids, ',');
            //判断当前用户是否存在宝箱
            $user_box_count = self::getGameUserBoxCount($themeid,$uid);
            if ($user_box_count>1){
                //获取当前用户的最大宝箱 获取宝箱id
                $user_last_box_flag = self::getUserLastBox($themeid,$uid);
                $user_last_box_id = $user_last_box_flag['gb_id'];
                $new_sql = " select a.gub_id,a.gb_id,a.uid,b.avatar,b.username,b.nickname,b.mobile,c.name,c.img,d.friend_view,d.name as box_name from "
                    .tablename(game_user_box)." as a ".
                    " LEFT JOIN ".tablename(t_fans)." as b on a.themeid=b.themeid ".
                    " LEFT JOIN ".tablename(game_prize)." as c on a.p_id=c.p_id and a.themeid=c.themeid".
                    " LEFT JOIN ".tablename(game_box)." as d on a.gb_id=d.gb_id and a.themeid=d.themeid".
                    " order by a.gb_id asc  where a.themeid=$themeid and a.gb_id < $user_last_box_id and a.uid in( ". $uids." )";
                $new_res = db_select($new_sql);
                if ($new_res){
                    $check_friend_uids_arr = [];
                    foreach ($new_res as $key =>$val){
                        array_push($check_friend_uids_arr,$val['uid']);
                    }
                }
            }

            $can_send_uid_arr = array_intersect($no_send_uids,$check_friend_uids_arr);
            $page = '';
            $user_name = isset($fans['username'])?$fans['username']:$fans['nickname'];
            $msg = '您的好友'.$user_name.'获得'.$user_last_box_flag['name'];
            for ($i=1;$i<=20;$i++){
                Koi::sendWxMsg($can_send_uid_arr[$i-1],$system,$page,$themeid,$msg);
            }
        }
    }


    //获取用户好友的共享宝箱信息
    public static function getFriendBoxList($themeid,$uid)
    {
        $has_friend_shares_box = false;
        $friend_shares_box_info = [];


        $sql  = "SELECT * FROM " . tablename (t_record) .
            " WHERE themeid=$themeid and uid='{$uid}' and type<>8 order by id desc limit 1";
        $team_info = db_find($sql);
        if ($team_info){
            $pid = $team_info['pid'];
            $sql =  "SELECT * FROM  " . tablename ( t_record ) .
                "  WHERE (type = 0 or type = 1) and status > 0 and pid =$pid order by type asc,id asc";
            $list = db_select ($sql);
            if (count($list)>=2){
                $uids = '';
                foreach ($list as $k => $v){
                    if ($v['uid']!=$uid){
                        $friend_level_detail = Game::getGameUserDetail($themeid,$v['uid']);
                        if ($friend_level_detail){
                            $uids.=$v['uid'].',';
                        }
                    }
                }
                $uids = rtrim($uids,',');
                //判断当前用户是否存在宝箱
                $user_box_count = self::getGameUserBoxCount($themeid,$uid);

                if ($user_box_count<=0){
                    if ($uids){
                        $new_sql = " select a.gub_id,a.gb_id,a.uid,b.avatar,b.username,b.nickname,b.mobile,c.name,c.img,d.friend_view,d.name as box_name from "
                            .tablename(game_user_box)." as a ".
                            " LEFT JOIN ".tablename(t_fans)." as b on a.themeid=b.themeid ".
                            " LEFT JOIN ".tablename(game_prize)." as c on a.p_id=c.p_id and a.themeid=c.themeid".
                            " LEFT JOIN ".tablename(game_box)." as d on a.gb_id=d.gb_id and a.themeid=d.themeid".
                            " where a.themeid=$themeid and a.uid in ( ". $uids." ) order by a.gb_id asc";
                        $new_res = db_select($new_sql);
                        if ($new_res && $new_res[0]){
                            $item = $new_res[0];
                            $has_friend_shares_box = true;
                            $friend_shares_box_info = [
                                'avatar'=>$item['avatar'],
                                'nickname'=>$item['nickname'],
                                'username'=>$item['username'],
                                'content1'=>'我已开启',
                                'content2'=>$item['box_name'],
                                'content3'=>'获得',
                                'content4'=>$item['name'],
                                'icon'=>'',
                                'gb_id'=>$item['gb_id'],
                                'btn_txt'=>'邀您共享宝箱福利',
                                'tips'=>'注：从现在仅需'.$item['friend_view'].'位好友到访，您就可获得此宝箱'
                            ];
                        }
                    }
                }else{
                    $user_last_box_flag  = self::getUserLastBox($themeid,$uid);
                    $user_last_box_id = $user_last_box_flag['gb_id'];
                    if ($uids && $user_last_box_id!=3 && $user_last_box_flag['is_end']==1){
                        $new_sql = " select a.gub_id,a.gb_id,a.uid,b.avatar,b.username,b.nickname,b.mobile,c.name,c.img,d.friend_view,d.name as box_name from "
                            .tablename(game_user_box)." as a ".
                            " LEFT JOIN ".tablename(t_fans)." as b on a.themeid=b.themeid ".
                            " LEFT JOIN ".tablename(game_prize)." as c on a.p_id=c.p_id and a.themeid=c.themeid".
                            " LEFT JOIN ".tablename(game_box)." as d on a.gb_id=d.gb_id and a.themeid=d.themeid".
                            "  where a.themeid=$themeid and a.gb_id > $user_last_box_id and a.uid in( ". $uids." ) order by a.gb_id asc ";
                        $new_res = db_select($new_sql);
                        if ($new_res && $new_res[0]){
                            $item = $new_res[0];
                            $has_friend_shares_box = true;
                            $friend_shares_box_info = [
                                'avatar'=>$item['avatar'],
                                'nickname'=>$item['nickname'],
                                'username'=>$item['username'],
                                'content'=>'我已开启'.$item['box_name'].'获得'.$item['name'],
                                'icon'=>'',
                                'gb_id'=>$item['gb_id'],
                                'btn_txt'=>'邀您共享宝箱福利',
                                'tips'=>'注：从现在仅需'.$item['friend_view'].'位好友到访，您就可获得此宝箱'
                            ];
                        }
                    }
                }
            }
        }

//        $sql = " select a.themeid,b.uid,b.nickname,b.mobile from ".tablename(game_life)." as a ".
//            " LEFT JOIN ".tablename(t_fans)." as b on a.uid=b.uid and a.themeid=b.themeid ".
//            " where  a.fromuid='{$uid}' and a.themeid=$themeid and a.type='friend' group by a.uid";
//        $res = db_select($sql);
//        $uids = '';
//        foreach ($res as $k =>$v){
//            if ($v['uid']!=$uid){
//                $friend_level_detail = Game::getGameUserDetail($themeid,$v['uid']);
//                if ($friend_level_detail){
//                    $uids.=$v['uid'].',';
//                }
//            }
//        }
//        $uids = rtrim($uids,',');

        return ['has_friend_shares_box'=>$has_friend_shares_box,'friend_shares_box_info'=>$friend_shares_box_info];

    }

    //获取团成员进度
    public static function getTeamFriend($themeid,$uid)
    {
        $list1 = [];
        $has_team = false;
        $sql  = "SELECT * FROM " . tablename (t_record) .
            " WHERE themeid=$themeid and uid='{$uid}' and type<>8 order by id desc limit 1";
        $team_info = db_find($sql);
        if (!$team_info){
            $list1 = [];
        }else{
            $pid = $team_info['pid'];
            $sql =  "SELECT * FROM  " . tablename ( t_record ) .
                "  WHERE (type = 0 or type = 1) and status > 0 and pid =$pid order by type asc,id asc";
            $list = db_select ($sql);
            if (count($list)<2){
                $list1 = [];
            }else{
                foreach ($list as $k => $v)
                {
                    if ($v['uid']!=$uid){
                        $is_leader = false;
                        if ($v['type']==0){
                            $is_leader = true;
                        }
                        $friend_uid = $v['uid'];
                        $prize_box_info = self::getFriendPrizeInfo($themeid,$uid,$friend_uid);
                        $temp_arr = [
                            'avatar'=>$v['avatar'],
                            'nickname'=>$v['nickname'],
                            'username'=>$v['username'],
                            'mobile'=>$v['mobile'],
                            'uid'=>$friend_uid,
                            'is_leader'=>$is_leader,
                            'friend_has_level'=>$prize_box_info['friend_has_level'],
                            'friend_has_box'=>$prize_box_info['friend_has_box'],
                            'friend_level_prize'=>$prize_box_info['friend_level_prize'],
                            'friend_box_prize'=>$prize_box_info['friend_box_prize'],
                            'can_shares_box'=>$prize_box_info['can_shares_box'],
                            'shares_box_tips'=>$prize_box_info['shares_box_tips']
                            ];
                        array_push($list1,$temp_arr);
                    }
                }
                $has_team = true;
            }
        }
        return ['has_team'=>$has_team,'team_list'=>$list1];
    }

    /**
     * Desc:格式化关卡的sort字符串
     * User:cbw
     * Time: 2021/2/25 11:16 下午
     * @param $arr
     * @return string
     */
    public static function formatLevelBySorts($arr)
    {
        $sorts = '';
        if (is_array($arr)){
            foreach ($arr as $k =>$v)
            {
                $sorts .= $v.',';
            }
        }
        if ($sorts){
            $sorts = rtrim($sorts,",");
        }
        return $sorts;
    }
    //获取格式化关卡提示奖品关卡信息
    public static function formatBindSorts($item)
    {
        $themeid = $item['themeid'];
        $sorts = $item['bind_show_sorts'];
        $arr = [];
        if ($sorts){
            $list = self::getGameListBySorts($themeid,$sorts);
            $temp = [];
            foreach ($list as $k =>$v){
                $temp=[
                    'img'=>$v['img'],
                    'title'=>mb_substr($v['name'], 0, 5, 'utf8')
                ];
                array_push($arr,$temp);
            }
        }
        shuffle($arr);
        return $arr;
    }


}